﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Petroleum.Tool;
using HZH_Controls.Controls;
using Petroleum.DB.Model;
using Petroleum.Model;
using HZH_Controls.Forms;
using Petroleum.UC.Public;
using Newtonsoft.Json;
using System.Data.SQLite;

namespace Petroleum.UC
{
    public partial class Step7 : UserControl
    {
        FormMain main = new FormMain();

        /// <summary>
        /// 钻井液List
        /// </summary>
        List<DepthRangeModel> depthList = new List<DepthRangeModel>();

        List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
        DepthRangeModel currentdeepth = new DepthRangeModel();//编辑时用到的模型
        DepthRangeModel moredeepth = new DepthRangeModel();//
        DepthRangeModel depthRangeModel = new DepthRangeModel();
        bool hassave = false;
        /// <summary>
        /// 套管List
        /// </summary>
        List<Casing> casingList = Session.CasingProgram.CasingList;

        /// <summary>
        /// 当前操作对象
        /// </summary>
        Casing casing = new Casing();

        public Step7(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            Load();
        }
        void Load()
        {
            List<KeyValuePair<string, string>> spudCom = casingList.Select(m =>
                new KeyValuePair<string, string>(m.SpudNo, m.SpudNo)
            ).ToList();
            cmdCasing.Source = spudCom;
            cmdCasing.SelectedIndex = 1;
        }
        private void ucBtnExt2_BtnClick(object sender, EventArgs e)
        {
            this.verificationComponent1.Verification();
            if (lstErrorControl.Count == 0)
            {
                depthRangeModel = new DepthRangeModel();
                depthRangeModel.API = currentdeepth.API;
                depthRangeModel.FunnelViscosity = currentdeepth.FunnelViscosity;
                depthRangeModel.Cake = currentdeepth.Cake;
                depthRangeModel.PH = currentdeepth.PH;
                depthRangeModel.Sandcontent = currentdeepth.Sandcontent;
                depthRangeModel.Frictioncoefficient = currentdeepth.Frictioncoefficient;
                depthRangeModel.initial = currentdeepth.initial;
                depthRangeModel.final = currentdeepth.final;
                depthRangeModel.Plastic = currentdeepth.Plastic;
                depthRangeModel.YieldPiont = currentdeepth.YieldPiont;
                depthRangeModel.Solidcontent = currentdeepth.Solidcontent;
                depthRangeModel.Bentonite = currentdeepth.Bentonite;
                depthRangeModel.SpudNo = cmdCasing.SelectedValue;
                depthRangeModel.BitSize = txtBitSize.InputText;
                depthRangeModel.DepthRange = txtDepthRange.InputText;
                depthRangeModel.DepthRange1 = txtDepthRange1.InputText;
                depthRangeModel.Density = txtDensity.InputText;
                depthRangeModel.DrillingFluidType = txtDrillingFluidType.InputText;
                depthRangeModel.Formation = txtFormation.InputText;
                depthList.Add(depthRangeModel);
                depthList.Remove(currentdeepth);
                this.dgv.DataSource = depthList;
                currentdeepth = new DepthRangeModel();
            }
            lstErrorControl.Clear();
            this.Invalidate(true);
        }
        private void ucDataGridView1_Load(object sender, EventArgs e)
        {
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "SpudNo", HeadText = "Spud No", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "BitSize", HeadText = "BitSize", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "DepthRange", HeadText = "DepthRange", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "DrillingFluidType", HeadText = "DrillingFluidType", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Formation", HeadText = "Formation", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Density", HeadText = "DepthRangeDensity", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "FunnelViscosity", HeadText = "FunnelViscosity", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Cake", HeadText = "Cake", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "PH", HeadText = "PH", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Sandcontent", HeadText = "Sandcontent", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Frictioncoefficient", HeadText = "Frictioncoefficient", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "initial", HeadText = "initial", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "final", HeadText = "final", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Plastic", HeadText = "Plastic", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "YieldPiont", HeadText = "YieldPiont", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Solidcontent", HeadText = "Solidcontent", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Bentonite", HeadText = "Bentonite", Width = 200, WidthType = SizeType.Absolute });
            this.dgv.Columns = lstCulumns;
            this.dgv.DataSource = depthList;
        }
        private void btnDown_BtnClick(object sender, EventArgs e)
        {
            //casing.DepthRangeModelList = depthList;//套管的钻井液数据
            Session.DepthRangeModelList = depthList;
            hassave = true;
            main.AddControl(new Step8EstimatedMaterialConsumptionAndCostDesign(main));
        }
        private void cmdCasing_SelectedChangedEvent(object sender, EventArgs e)
        {
            //获取对应的套管
            string spud = cmdCasing.SelectedText;
            casing = casingList.FirstOrDefault(m => m.SpudNo == spud);
            if (casing != null)
            {
                txtBitSize.InputText = casing.BitSize;      //BitSize赋值
                txtDepthRange.InputText = casing.HoleDepth;      //BitSize赋值
                txtDepthRange1.InputText = casing.HoleDepth;      //BitSize赋值
            }
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step6BHADesign(main));
        }
        List<Control> lstErrorControl = new List<Control>();
        private void verificationComponent1_Verificationed(VerificationEventArgs e)
        {
            if (!e.IsVerifySuccess)
            {
                lstErrorControl.Add(e.VerificationControl);
            }
        }
        private void btndel_BtnClick(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                int row = dgv.SelectRow.RowIndex;
                if (row > -1)
                {
                    depthList.RemoveAt(row);
                    this.dgv.DataSource = depthList;
                    return;
                }
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要删除的行");
        }
        //更多参数添加
        private void ucBtnExt3_BtnClick(object sender, EventArgs e)
        {
            Step7Info step7 = new Step7Info(currentdeepth);
            AlertForm alert = new AlertForm(step7, "更多参数设置", GetMoreInfo);
            alert.ShowDialog();
        }
        void GetMoreInfo(DepthRangeModel list)
        {
            if (list != null)
            {
                this.currentdeepth = list;
            }
        }
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            var depthListOld = main.projects.Where(n => n.TableName == TableNames.depthList.ToString()).FirstOrDefault();
            if (depthListOld != null)
            {
                Session.DepthRangeModelList = JsonConvert.DeserializeObject<List<DepthRangeModel>>(depthListOld.TableData);
                string spud = cmdCasing.SelectedText;
                var depth = Session.DepthRangeModelList.Where(m => m.SpudNo == spud).ToList();
                if (depth != null)
                {
                    dgv.DataSource = depth;
                    var dep = depthList.Where(m => m.SpudNo == spud).ToList();
                    if (dep.Count <= 0)
                        depthList.AddRange(depth);
                }
            }
        }
        private void btnsave_BtnClick(object sender, EventArgs e)
        {
            if (!hassave)
                Session.DepthRangeModelList = depthList;
            SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
            var projects = EntityTranslate.Translate<SessionData>(reader);
            var FormationPressure = projects.Where(n => n.TableName == TableNames.depthList.ToString()).FirstOrDefault();
            if (FormationPressure != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.depthList.ToString(), JsonConvert.SerializeObject(Session.DepthRangeModelList));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.depthList.ToString(), JsonConvert.SerializeObject(Session.DepthRangeModelList), 1);
            FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
        }
        private void StripMenuEdit_Click(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                currentdeepth = depthList[dgv.SelectRow.RowIndex];
                cmdCasing.SelectedValue = currentdeepth.SpudNo;
                txtBitSize.InputText = currentdeepth.BitSize;
                txtDepthRange.InputText = currentdeepth.DepthRange;
                txtDepthRange1.InputText = currentdeepth.DepthRange1;
                txtDensity.InputText = currentdeepth.Density;
                txtDrillingFluidType.InputText = currentdeepth.DrillingFluidType;
                txtFormation.InputText = currentdeepth.Formation;
            }
        }
    }
}
